$(function(){
	var path = $("#webPath").val();
	//“主机列表”，“主机群组列表”，“模板列表”按钮样式修改 
	$(".includeul:first-child li:eq(2)", window.parent.document).addClass("current");
	$(".includeul:first-child li:eq(1)", window.parent.document).removeClass("current");
	$(".includeul:first-child li:eq(0)", window.parent.document).removeClass("current");
	//初始化列表查询
    paging(1);
    var groupid=$("#searchTemplateByHostGroup > option:selected").val();
    if(groupid!=0){
    	searchTemplateByHostGroup();
    }
    //1 绑定添加按钮
    $('.addbtn').click(function(){
        layer.open({
        	extend:['skin/layer_iframe.css'],
        	skin:'layer-ext-skin-layer_iframe.css',
            type:2,
            title:'添加页面',
            area:['800px','500px'],
            content:'initAddTemplate.do',
            maxmin: true,
            shadeClose: true
        });
    });
    
    //4 绑定查询按钮
    $('.sel_btn').click(function(){
    	paging(1);
    });
    
    //6.删除选定的，多项删除
    $('.delbtn').click(function(){
    	del_select_func(this);
    });
    
    //通过群组获取模板列表
    $("#searchTemplateByHostGroup").change(function() {
    	searchTemplateByHostGroup();
    });
});

searchTemplateByHostGroup=function(){
	var groupid = $("#searchTemplateByHostGroup > option:selected").val();
	if(groupid==0){
		paging(1);
		return;
	}
	$.ajax({
		url:webPath+"/radar/template/searchTemplateByHostGroup.do",
		type:"post",
		dataType:"json",
		async:false,
		data:{
			"groupid":groupid
		},
		error:function(){
			layer.msg("出错了",{time:1500});
		},
		success:function(jsons){
			if(jsons.code == 500){
        		//alert(jsons.eroorMessage);
                layer.msg(jsons.errorMessage,{time:2000});
        	}else if (jsons.code == 201){
        		//alert("数据为空！");
        		$("#pageTable").html("");
                layer.msg("列表为空",{time:2000});
        	}else{
        		$("#pageTable").html("");
        		unifiedPaging(jsons.pageData, "see");
        	}
		}
	});
}

checkallmethod=function(){
    $('.checkAll').click(function(){
        if($(this).is(':checked')){
            $('.content_table .checkinput').attr('checked','checked');
            $(this).attr('title','取消全选')
        }else{
            $('.content_table .checkinput').removeAttr('checked');
            $(this).attr('title','全选')
        }
    })
}

function paging(pageNo){
	var pageSize = $("#pageSize  option:selected").text();
    $.ajax({
        url: "pagingTemplate.do",
        type: "post",
        dataType: 'json',
        async: false,
        data: {
        	"iPageSize": pageSize,
            "iPageNo": pageNo
        },
        error: function (data) {
        	layer.msg('出错了！！',{time:2000});
        },
        success: function (jsons) {
        	if(jsons.code == 500){
        		//alert(jsons.eroorMessage);
                layer.msg(jsons.errorMessage,{time:2000});
        	}else if (jsons.code == 201){
        		//alert("数据为空！");
                layer.msg("数据为空！",{time:2000});
        	}else{
        		unifiedPaging(jsons.pageData, "see");
        	}
        }
    })
    checkallmethod();
}

see_btn=function(obj){
    //绑定查看按钮
	var Id=$(obj).parent().parent().attr("ID");
	window.location.href=webPath+"/radar/template/showApplications.do?templateid="+Id;
//	layer.open({
//		extend:['skin/layer_iframe.css'],
//        skin:'layer-ext-skin-layer_iframe.css',
//        type:2,
//        title:'查看监控历史数据',
//        area:['800px','600px'],
//        content:'showMonitoredDatas.do?hostid='+Id,
//        maxmin: true,
//        shadeClose: true
//    });
}

/**
 * 点击列表右侧修改按钮
 */
edit_func=function(obj){
    var Id=$(obj).parent().parent().attr('ID');
    var path = $("#webPath").val();
    layer.open({
    	extend:['skin/layer_iframe.css'],
        skin:'layer-ext-skin-layer_iframe.css',
        type:2,
        title:'修改页面',
        area:['800px','500px'],
        content:'initEditTemplate.do?templateid='+Id,
        maxmin: true,
        shadeClose: true
    });
}

/**
 * 点击右侧删除按钮
 * @param obj
 */
delete_func=function(obj){
    var Id=$(obj).parent().parent().attr('ID');
    if(confirm('确定删除?')){
        $.ajax({
            url:"delTemplate.do",
            type:"post",
            dataType: 'json',
            data:{
                "templateid":Id
            },
            beforeSend:function(){
                $(obj).attr("disabled","disabled");
            },
            error:function(data){
            	layer.msg('出错了！！',{time:2000});
            },
            success:function(result){
                if(result.code=='200'){
                    layer.msg(result.errorMessage, {time:1300});
                    $('.checkAll').removeAttr('checked');
                    paging(1);
                }else{
                    layer.msg(result.errorMessage, {time:1000});
                    $(obj).removeAttr("disabled");
                }
            }
        });
    }
}

function del_select_func(obj){
	var checkedDel = "";
	var arr=document.getElementsByName("checkradio");
	 	
 	for(i=0;i<arr.length;i++){
		if(arr[i].checked){
		var a = arr[i].value;
		checkedDel+=a+",";
		}
	}
	if(checkedDel==""){
		layer.msg('先勾选选择框，再点击删除按钮！', {time:1300});
	}else{
		if(confirm('确定删除所有选中关系')){
			$.ajax({
		        url:"delSelectTemplate.do",
		        type:"post",
		        dataType: 'json',
		        data:{
		            "checkedDel":checkedDel
		        },
		        beforeSend:function(){
		            
		        },
		        error:function(data){
		        	layer.msg('出错了！！',{time:2000});
		        },
		        success:function(result){
		            if(result.code=='200'){
		                layer.msg(result.errorMessage, {time:1300});
		                $('.checkAll').removeAttr('checked');
		                paging(1);
		            }else{
		                layer.msg(result.errorMessage, {time:1000});
		                $(obj).removeAttr("disabled");
		            }
		        }
			});
		}
	}
}

